package com.example.kwokg.meishi;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.astuetz.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.Bmob;

public class MainActivity extends AppCompatActivity {

    private String customColor="#3F51B5";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, Constants.Bmob_APPID);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

//        初始化抽屉和ViewPager
        initDrawer();
        initVp();
    }

    //对ViewPager的操作
    private void initVp() {
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabStrip);//滑动条控件
        ViewPager mVp = (ViewPager) findViewById(R.id.vp);
        String[] titles=getResources().getStringArray(R.array.vp_item);//各个fragment的标题
        List<Fragment> fragments=new ArrayList<>();

        //TODO:Bundle传值给fragment
        Bundle bundle=new Bundle();
        //获取vp的fragment对象
        for (String title : titles) {
            MainFragment fragment = new MainFragment();
            fragments.add(fragment);
        }
        mVp.setAdapter(new MyVpAdapter(getSupportFragmentManager(),fragments, titles));

        //设置TabStrip
        tabs.setViewPager(mVp);//关联适配器
        tabs.setBackgroundColor(Color.parseColor(customColor));
        tabs.setIndicatorHeight(5);//滑动条高度
        tabs.setDividerColor(Color.parseColor(customColor));//分割线颜色
        tabs.setTextColor(Color.WHITE);//字体颜色
        tabs.setIndicatorColor(Color.WHITE);//滑动条颜色

    }

    //对抽屉进行操作
    private void initDrawer() {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setBackgroundColor(Color.parseColor(customColor));
        mToolbar.setTitle("美视");
        mToolbar.setTitleTextColor(Color.WHITE);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawer,mToolbar,R.string.open,R.string.close);
        toggle.syncState();
        drawer.addDrawerListener(toggle);

        //抽屉内的ListView
        ListView drawer_lv = (ListView) findViewById(R.id.drawer_lv);
        List<Map<String,Object>> list=new ArrayList<>();
        String[] text=getResources().getStringArray(R.array.menu_text);
        int[] imgs=getResources().getIntArray(R.array.menu_imgs);
        //TODO:图片
        for ( int i = 0 ; i < text.length ; i++){
            Map<String,Object> map=new HashMap<>();
            map.put("text",text[i]);
            map.put("imgs",imgs[i]);
            list.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.menu_item,new String[]{"imgs","text"},new int[]{R.id.img_dw,R.id.tv_dw});
        drawer_lv.setAdapter(adapter);
    }

    //注册按钮点击事件
    public void register_dw(View view) {
        startActivity(new Intent(this,Register.class));
        overridePendingTransition(0,0);
    }

    //登录按钮点击事件
    public void login_dw(View view) {
        startActivity(new Intent(this,Login.class));
        overridePendingTransition(0,0);
    }
}
